package org.auf.model.jdbc;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Collection;
import java.util.LinkedList;

import org.apache.log4j.Logger;
import org.auf.model.dao.IEventDAO;
import org.auf.to.Event;

public class EventDAOJDBC implements IEventDAO {
	
	static Logger log = Logger.getLogger(EventDAOJDBC.class.getName());

    private static EventDAOJDBC INSTANCE = null;
    
    private EventDAOJDBC() {}
    
    private synchronized static void createInstance() {
        if (INSTANCE == null) { 
            INSTANCE = new EventDAOJDBC();
        }
    }
 
    public static IEventDAO getInstance() {
        if (INSTANCE == null) createInstance();
        return INSTANCE;
    }

	@Override
	public boolean insertEvent(Event event) {
		int updates=0;
		ConnectionManager cm = ConnectionManager.getInstance();
		Connection db = cm.checkOut();
		
		try{
			PreparedStatement st = db.prepareStatement("INSERT INTO tblevent (id,title,description,volunteersSlots,geeksSlots,owner,dateLimit) values (?,?,?,?,?,?,?)");
			st.setString(1, event.getId());
			st.setString(2, event.getTitle());
			st.setString(3, event.getDescription());
			st.setInt(4, event.getVolunteersSlots());
			st.setInt(5, event.getGeeksSlots());
			st.setString (6 ,event.getOwner().getId());
			st.setDate (7 ,new java.sql.Date(event.getDateLimit().getTime()));
			updates =st.executeUpdate();
		
		}catch(Exception e){updates=-1;log.error("Error al generar la sentencia SQL y ejecutarla: "+e);}

		cm.checkIn(db);
		// TODO Auto-generated method stub
		return updates==1;
	}

	@Override
	public Collection<Event> selectAllEvent() {
		Collection<Event> sol = new LinkedList<Event>();
		
		ConnectionManager cm = ConnectionManager.getInstance();
		Connection db = cm.checkOut();
		
		try{
			PreparedStatement st = db.prepareStatement("SELECT * FROM tblevent");
			ResultSet rs = st.executeQuery();
		
			while (rs.next()){
				Event event = new Event();
				event.setId( rs.getString("id"));
				event.setTitle(rs.getString("title"));
				event.setDescription(rs.getString("description"));
				event.setVolunteersSlots(rs.getInt("volunteersSlots"));
				event.setGeeksSlots(rs.getInt("geeksSlots"));
				event.setOwner(UserDAOJDBC.getInstance().selectUserByid(rs.getString("owner")));
				event.setDateLimit(rs.getDate("dateLimit"));
				sol.add(event);
			}
		
		}catch(Exception e){sol=null;log.error("Error al generar la sentencia SQL y ejecutarla: "+e);}

		cm.checkIn(db);
		// TODO Auto-generated method stub
		return sol;
	}

	@Override
	public Event selectEvent(String id) {
		Event event = new Event();
		
		ConnectionManager cm = ConnectionManager.getInstance();
		Connection db = cm.checkOut();
		
		try{
			PreparedStatement st = db.prepareStatement("SELECT * FROM tblevent WHERE id=?");
			st.setString(1, id);
			ResultSet rs = st.executeQuery();
		
			if (rs.next()){
				event.setId( rs.getString("id"));
				event.setTitle(rs.getString("title"));
				event.setDescription(rs.getString("description"));
				event.setVolunteersSlots(rs.getInt("volunteersSlots"));
				event.setGeeksSlots(rs.getInt("geeksSlots"));
				event.setOwner(UserDAOJDBC.getInstance().selectUserByid(rs.getString("owner")));
				event.setDateLimit(rs.getDate("dateLimit"));
				if (rs.next())event=null;
			}else event=null;
		
		}catch(Exception e){event=null;log.error("Error al generar la sentencia SQL y ejecutarla: "+e);}

		cm.checkIn(db);
		// TODO Auto-generated method stub
		return event;
	}

}
